__author__ = 'wenke1'

import time

def handleFileByKey(file):
    f1 = open(file)
    f2 = open('temp', 'w')
    for line1 in f1.readlines():
        str_split1 = line1.split('\t', 2)
        if 1 < float(str_split1[2]) < 2:
            f2.writelines(line1)
    f1.close()
    f2.close()

def handleFileBytime(file):
    ft = open(file, 'r')
    fh = open("sorted_temp", 'w')
    lines = ft.readlines()
    lineNum = len(lines)
    for i in range(lineNum):
        for j in range(lineNum - 1, i, -1):
            iStrtime, jStrtime = lines[i].split('\t')[0].strip(), lines[j].split('\t')[0].strip()
            iCtime, jCtime = time.strptime(iStrtime,"%b %d %H:%M:%S %Y"), time.strptime(jStrtime,"%b %d %H:%M:%S %Y")
            if iCtime > jCtime:
                lines[i], lines[j]=lines[j], lines[i]
    fh.writelines(lines)
    ft.close()
    fh.close()

if __name__ == "__main__":
    if not handleFileByKey('data1'):
        handleFileBytime('temp')
